package com.yun.util.yun.utils.date;

/**
 *
 */
public class ScheduleUtils {

    public static String getExpression (Integer exeCycle, String cycleValue, String exeTime) {
        String expression ="";
        //判断执行周期是否为  月  周   日   或   仅一次
        if(exeCycle==0){
            //每月15号10点15  0 15 10 15 * ?
            String[] time = exeTime.split(":");
            if(time.length==2){
                expression = "0 "+ time[1] + " " + time[0] + " " + cycleValue +" * ?";
            }else{
                expression = time[2] + " " + time[1] + " " + time[0] + " " + cycleValue +" * ?";
            }
            //scheduleInfo.setExeVal(cycleValue);
        }else if(exeCycle==1){
            // 3月分每周三下午的 2点10分触发       0 10 14 ? 3 WED
            String[] time = exeTime.split(":");
            if(time.length==2){
                expression = "0 "+ time[1] + " "+ time[0] +" ? * " + cycleValue;
            }else{
                expression = time[2] + " " + time[1] + " " + time[0] + " ? * "+ cycleValue;
            }
        }else if(exeCycle==2){
            //每天10点15分触发    0 15 10 ? * *
            String[] time = exeTime.split(":");
            if(time.length==2){
                expression = "0 "+ time[1] + " " + time[0] + " ? * *";
            }else{
                expression = time[2] + " " + time[1] + " " + time[0] + " ? * *";
            }
        }else{
            String[] times = exeTime.split(" ");
            String[] date = times[0].split("-");
            String[] time = times[1].split(":");
            expression = time[2] + " " + time[1] + " " + time[0] + " " + date[2] + " " + date[1] + " ? " + date[0];
        }
        return expression;
    }

    public static String[] convertExpression(String expression) {

        String[] exp = expression.split(" ");
        String exeTime = exp[2] + ":" + exp[1];
        //如果是零,说明原来在界面上选择到分
        if(!exp[0].equals("0")) {
            exeTime = exeTime + ":" + exp[0];
        }        
        String exeCycle =  "";
        String cycVal = "";
        if(exp[4].equals("*") && exp[5].equals("?")){
            exeCycle = "0";
            cycVal = exp[3];
        } else if(exp[3].equals("?") && exp[4].equals("*") && exp[5].equals("*")){
            //一定要将此判断写在else if(exp[3].equals("?") && exp[4].equals("*"))的前面，因为此条件是它的进一步匹配
            exeCycle = "2";
        } else if(exp[3].equals("?") && exp[4].equals("*")){
            exeCycle = "1";
            cycVal = exp[5];
        }else{
            exeCycle = "3";
            //上面已经算了时间,此处在前面加上日志
            exeTime = exp[6] + "-" + exp[4] + "-" + exp[3] + " " + exeTime;
        }
        String[] retArray = new String[3];
        retArray[0] = exeCycle;
        retArray[1] = cycVal;
        retArray[2] = exeTime;
        return retArray;
    }
}
